package api;

import cn.hutool.json.JSONUtil;
import common.ApplicationVariable;
import model.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/logout")
public class LoginOutServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType(ApplicationVariable.RESPONSE_CONTENT_TYPE);
        int state = -1;
        HttpSession session = req.getSession(false);
        if (session != null && session.getAttribute(ApplicationVariable.SESSION_USER_KEY) != null) {
            // 登录状态，移除登录
//            session.setAttribute(ApplicationVariable.SESSION_USER_KEY,
//                    null);
            session.removeAttribute(ApplicationVariable.SESSION_USER_KEY);
            state = 200;
        }
        resp.getWriter().println(JSONUtil.toJsonStr(
                new ResultData(state, "", "")
        ));
    }
}